﻿using System.Collections.Generic;

namespace Gp.Scripts.Core
{
    /// <summary>
    /// 在C#中实现的弱类型对象。主要功能是作为上下文参数传递。
    /// </summary>
    public class WeakTypeObject
    {
        private readonly Dictionary<string, object> _fieldDic = new();
        public object this[string fieldName]
        {
            get => _fieldDic.GetValueOrDefault(fieldName, null);
            set => _fieldDic[fieldName] = value;
        }


        public T Get<T>(string fieldName)
        {
            if (!_fieldDic.ContainsKey(fieldName)) return default;
            var temp = _fieldDic[fieldName];
            return temp is not T res ? default : res;
        }

        public void Set<T>(string fieldName, T value) => _fieldDic[fieldName] = value;


        public override string ToString()
        {
            return _fieldDic.ToString();
        }


        public override bool Equals(object obj)
        {
            return _fieldDic.Equals(obj);
        }

        public override int GetHashCode()
        {
            return _fieldDic.GetHashCode();
        }
    }
}